package com.zhangxun.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangxun.domain.ProdProp;
import com.zhangxun.domain.ProdPropValue;
import com.zhangxun.service.ProdPropService;
import com.zhangxun.service.ProdPropValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
@RequestMapping("/prod/spec")
public class ProdPropController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;

    @GetMapping("/list")
    @ApiOperation("商品规格管理接口")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<List<ProdProp>> list(){

        return ResponseEntity.ok(prodPropService.list());
    }

    @GetMapping("/listSpecValue/{propId}")
    @ApiOperation("商品规格属性值接口")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<List<ProdPropValue>> listSpecValue(@PathVariable("propId") Long propId){
        LambdaQueryWrapper<ProdPropValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProdPropValue::getPropId,propId);
        List<ProdPropValue> list = prodPropValueService.list(wrapper);

        return ResponseEntity.ok(list);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询规格接口，规格名称模糊匹配")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Page<ProdProp> page(Page<ProdProp> page,ProdProp prodProp){

        page = prodPropService.page(page,new LambdaQueryWrapper<ProdProp>()
                .like(StringUtils.hasText(prodProp.getPropName()),ProdProp::getPropName,prodProp.getPropName())
        );

        return page;

    }

}
